package hhm.android.myframe

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [BlankFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class BlankFragment : Fragment {
    private var param1: String? = null
    private var param2: String? = null
    private var str: String
    lateinit var adapter: MessageSystemListAdapter

    constructor(text: String) {
        this.str = text
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = LayoutInflater.from(activity).inflate(R.layout.fragment_blank, container, false)
        val srl = view.findViewById<SmartRefreshLayout>(R.id.srl)
        val recycler = view.findViewById<RecyclerView>(R.id.recycler)

        srl.setEnableLoadMore(false)
        srl.setOnRefreshListener {
            val list = ArrayList<Int>()
            for (index in 100..110) {
                list.add(index)
            }
            adapter.addData(0, list)
            srl.finishRefresh()
            if (adapter.list.size >= 50) {
                srl.setEnableRefresh(false)
            }
        }

        val list = ArrayList<Int>()
        for (index in 1..10) {
            list.add(index)
        }

        recycler.layoutManager = LinearLayoutManager(activity)
        adapter = activity?.let { MessageSystemListAdapter(it, list) }!!
        recycler.adapter = adapter


        return view
    }
}